﻿using ETModel;
using System.Collections.Generic;
using UnityEngine;

namespace ETHotfix {
    public static class GameUnitFactory {
        public static void LoadUnitsByInfos(IEnumerable<UnitInfo> units) {
            foreach (var unitInfo in units) {
                LoadUnitByInfo(unitInfo);
            }
        }


        public static Unit LoadUnitByInfo(UnitInfo unitInfo) {

            UnitsComponent unitComponent = Game.Scene.GetComponent<UnitsComponent>();
            var unit = unitComponent.Get(unitInfo.UnitId);
            if (unit != null) {
                //id已存在,则直接返回
                return unit;
            }
            UnitConfig unitConfig = Game.Scene.GetComponent<ConfigComponent>().Get<UnitConfig>(unitInfo.UnitConfigId);
            unit = CreateUnit(unitInfo, unitConfig);
            unitComponent.Add(unit);
            return unit;
        }

        public static GameUnit CreateUnit(UnitInfo unitInfo, UnitConfig unitConfig) {

            GameObject unitGameObj = LoadGameObjectByUnitConfig(unitInfo, unitConfig);

            GameUnit unit = ComponentFactory.CreateWithId
                <GameUnit, GameObject, UnitInfo>(unitInfo.UnitId, unitGameObj, unitInfo);

            return unit;
        }


        /// <summary>
        /// 根据配置加载u3d对象
        /// </summary>
        /// <param name="unitConfig"></param>
        /// <returns></returns>
        public static GameObject LoadGameObjectByUnitConfig(UnitInfo unitInfo, UnitConfig unitConfig) {

            if (!string.IsNullOrEmpty(unitConfig.BundleName)
                 && !string.IsNullOrEmpty(unitConfig.BundlePrefabName)
                 && !string.IsNullOrEmpty(unitConfig.GameObjectRefName)) {
                //有配置资源,则从资源里加载模型
                return LoadGameObjectByBundle(unitConfig.BundleName, unitConfig.BundlePrefabName, unitConfig.GameObjectRefName);
            } else if (unitInfo.ShapeIndices.Count > 0) {
                //有传形状信息,则根据形状创建Mesh
                return CreateMeshByShape("id_" + unitInfo.UnitId, unitInfo.GetVertices(), unitInfo.GetIndices());
            }

            return null;
        }


        public static GameObject CreateMeshByShape(string name, Vector3[] vertices, int[] indices) {
            var mesh = new Mesh();
            mesh.vertices = vertices;
            mesh.triangles = indices;
            mesh.RecalculateNormals();

            var meshObj = new GameObject();
            meshObj.name = name;
            var mFilter = meshObj.AddComponent<MeshFilter>();
            mFilter.mesh = mesh;
            MeshRenderer meshRender = meshObj.AddComponent<MeshRenderer>();
            Shader shader = Shader.Find("Diffuse");
            meshRender.sharedMaterial = new Material(shader);

            return meshObj;
        }

        /// <summary>
        /// 根据配置加载u3d对象
        /// </summary>
        /// <param name="BundleName"></param>
        /// <param name="BundlePrefabName"></param>
        /// <param name="GameObjectRefName"></param>
        /// <returns></returns>
        public static GameObject LoadGameObjectByBundle(string BundleName, string BundlePrefabName, string GameObjectRefName) {

            ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
            GameObject bundleGameObject
                = (GameObject)resourcesComponent.GetAsset(BundleName, BundlePrefabName);
            GameObject prefab = bundleGameObject.Get<GameObject>(GameObjectRefName);
            var gameObj = UnityEngine.Object.Instantiate(prefab);

            return gameObj;
        }
    }
}